package com.pic4493.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.pic4493.comm.PubConst;
import com.pic4493.entities.EUser;
import com.ppcodes.imageloader.utils.L;
import com.ppcodes.imageloader.utils.StorageUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class UtiFile
{

   /**
    * 一次性读取全部文本文件内容到string
    * 
    * @param file
    * @return
    */
   public static String getOffLineData(File file)
   {
	  try
	  {
		 Long fileLengthLong = file.length();
		 byte[] fileContent = new byte[fileLengthLong.intValue()];
		 FileInputStream inputStream = new FileInputStream(file);
		 inputStream.read(fileContent);
		 inputStream.close();
		 return new String(fileContent);
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_getOffLineDataName(file) " + e);
	  }
	  return null;
   }

   public static void saveOffLineData(File file, String content)
   {
	  try
	  {
		 FileWriter writer = new FileWriter(file);
		 writer.write(content);
		 writer.close();
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_saveOffLineDate() " + e);
	  }
   }

   public static void saveLoginSession(Context context,EUser user)
   {
	  try
	  {
		  SharedPreferences pref = context.getSharedPreferences("session", Context.MODE_PRIVATE);
		  Editor editor = pref.edit();
		  editor.putString(PubConst.Setting.USERNAME, user.getUserName());
		  editor.putString(PubConst.Setting.PASSWORD, user.getPassWord());
          editor.commit();
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_saveLoginSession() " + e);
	  }
   }

   public static EUser loadLoginSession(Context context)
   {
	  try
	  {

	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_loadLoginSession() " + e);
	  }
	  return null;

   }

   public static File getExternalWallPaperDir()
   {
	  File dataDir = new File(new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data"), "com.pic4493.app");
	  return dataDir;
   }

   public static File getExternalCacheDir()
   {
	  File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
	  File appCacheDir = new File(new File(dataDir, "com.pic4493.app"), "cache");
	  if (!appCacheDir.exists())
	  {
		 try
		 {
			new File(dataDir, ".nomedia").createNewFile();
		 }
		 catch (IOException e)
		 {
			L.e(e, "Can't create \".nomedia\" file in application external cache directory");
		 }
		 if (!appCacheDir.mkdirs())
		 {
			L.w("Unable to create external cache directory");
			return null;
		 }
	  }
	  return appCacheDir;
   }

   public static void saveMyBitmap(Context context, Bitmap mBitmap)
   {
	  SimpleDateFormat tempDateTime = new SimpleDateFormat("yyyyMMddHHmmss");
	  String name = tempDateTime.format(new Date());
	  String path = Environment.getExternalStorageDirectory() + "/pictures/4493/";
	  // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
	  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
	  {
		 makeRootDirectory(path);
		 File f = new File(path + name + ".jpg");
		 try
		 {
			if (f.exists())
			{
			   f.delete();
			}
			f.createNewFile();
			FileOutputStream fOut = null;
			fOut = new FileOutputStream(f);
			mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
			fOut.flush();
			fOut.close();
			Toast.makeText(context, "图片已经保存到" + path + name + ".jpg", Toast.LENGTH_LONG).show();
		 }
		 catch (Exception e)
		 {
			Log.d("ERROR", "UtiFile_saveMyBitmap(bitName, mBitmap) " + e);
		 }
	  }
   }

   public static void makeRootDirectory(File path)
   {
	  try
	  {
		 if (!path.exists())
		 {
			path.mkdirs();
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_makeRootDirectory(filePath) " + e);
	  }
   }

   public static void makeRootDirectory(String filePath)
   {
	  File file = null;
	  try
	  {
		 file = new File(filePath);
		 if (!file.exists())
		 {
			file.mkdirs();
		 }
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_makeRootDirectory(filePath) " + e);
	  }
   }

   /**
    * 检查是否存在SDCard
    * 
    * @return
    */
   public static boolean hasSdcard()
   {
	  String state = Environment.getExternalStorageState();
	  if (state.equals(Environment.MEDIA_MOUNTED))
	  {
		 return true;
	  }
	  else
	  {
		 return false;
	  }
   }

   /**
    * 获取文件夹大小
    * 
    * @param file
    *           File实例
    * @return long 单位为M
    * @throws Exception
    */
   public static long getFolderSize(java.io.File file)
   {
	  try
	  {
		 long size = 0;
		 java.io.File[] fileList = file.listFiles();
		 for (int i = 0; i < fileList.length; i++)
		 {
			if (fileList[i].isDirectory())
			{
			   size = size + getFolderSize(fileList[i]);
			}
			else
			{
			   size = size + fileList[i].length();
			}
		 }
		 return size / 1048576;
	  }
	  catch (Exception e)
	  {
		 Log.d("ERROR", "UtiFile_getFolderSize(file) " + e);
	  }
	  return 0;
   }
}
